home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / tutorials / custEducation / opengl1 / lib / toolkit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  6.4 KB  |  250 lines

  1. /*
  2.  * Copyright 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18.  
  19. #include <aux.h>
  20. #include <auxPrivate.h>
  21.  
  22.  
  23. /*************************************************************************
  24.  *   Toolkit Global variables
  25.  **************************************************************************/
  26.  
  27. GLboolean          animation = AUX_OFF;
  28. GLboolean          redrawRequired = GL_FALSE;
  29.  
  30. static GLvoid      DefaultReshape( GLsizei, GLsizei );
  31.  
  32. GLvoid             (*displayFunc)( GLvoid ) = NULL;
  33. GLvoid             (*reshapeFunc)( GLsizei, GLsizei ) = DefaultReshape;
  34. GLvoid             (*idleFunc)( GLvoid ) = NULL;
  35. GLvoid             (*exposeFunc)( GLsizei, GLsizei ) = NULL;
  36.  
  37.  
  38.  
  39. /**************************************************************************
  40.  *   auxDispatchEvent() 
  41.  **************************************************************************/
  42.  
  43. GLvoid
  44. auxDispatchEvent( GLint device )
  45. {
  46.     GLsizei           width, height;
  47.  
  48.     AUX_EVENTREC      auxEvent;
  49.  
  50.     switch ( device ) {
  51.  
  52.         case AUX_CONFIG :
  53. #ifdef DEBUG2
  54.             printf("   Toolkit : Calling reshapeFunc() ...\n");
  55. #endif
  56.             auxGetSize( &width, &height );
  57.             (*reshapeFunc)( width, height );
  58.             redrawRequired = GL_TRUE;
  59.             break;
  60.  
  61.         case AUX_EXPOSE :
  62. #ifdef DEBUG2
  63.             printf("   Toolkit : Calling exposeFunc() ...\n");
  64. #endif
  65.             auxGetSize( &width, &height );
  66.             if (exposeFunc)
  67.             {
  68.                 (*exposeFunc)( width, height );
  69.             }
  70.             redrawRequired = GL_TRUE;
  71.             break;
  72.         
  73.         case AUX_DRAW :
  74.             redrawRequired = GL_TRUE;
  75.             break;
  76.  
  77.         case AUX_LEFTBUTTON :
  78.         case AUX_MIDDLEBUTTON :
  79.         case AUX_RIGHTBUTTON :
  80. #ifdef DEBUG2
  81.             printf("   Toolkit : Calling mouse functions ...\n");
  82. #endif
  83.             auxEvent.data[AUX_MOUSEX] = auxMouseX;
  84.             auxEvent.data[AUX_MOUSEY] = auxMouseY;
  85.  
  86.             {     MouseFunc    *tmp;
  87.  
  88.             if ( auxValue ) {
  89.                 auxEvent.event = AUX_MOUSEDOWN;
  90.                 tmp = mouseDown[device - AUX_BUTTON_MASK];
  91.             } else { 
  92.                 auxEvent.event = AUX_MOUSEUP;
  93.                 tmp = mouseUp[device - AUX_BUTTON_MASK];
  94.             }
  95.  
  96.             if ( tmp ) {
  97.                 redrawRequired = GL_TRUE;
  98.                 while ( tmp ) {
  99.                     (*tmp->func)( &auxEvent );
  100.                     tmp = tmp->next;
  101.                 }
  102.             }
  103.         }    break;
  104.  
  105.         case AUX_MOUSELOC : {
  106.             MouseFunc       *tmp = mouseLoc;
  107.  
  108.             auxEvent.event = AUX_MOUSELOC;
  109.             auxEvent.data[AUX_MOUSEX] = auxMouseX;
  110.             auxEvent.data[AUX_MOUSEY] = auxMouseY;
  111.             if ( tmp ) {
  112. #ifdef DEBUG2
  113.             printf("   Toolkit : Calling mouseLoc() ...\n");
  114. #endif
  115.                 redrawRequired = GL_TRUE;
  116.                 while ( tmp ) {
  117.                     (*tmp->func)( &auxEvent );
  118.                     tmp = tmp->next;
  119.                 }
  120.             }
  121.         }    break;
  122.  
  123.         case AUX_KEYEVENT : {
  124.             KeyFunc           *tmp = keyHead;
  125.  
  126.             if ( !auxValue )
  127.                 return;
  128.  
  129.             while ( tmp )
  130.                 if ( auxDevice == tmp->key ) {
  131. #ifdef DEBUG2
  132.             printf("   Toolkit : Calling KeyFunc() ...\n");
  133. #endif
  134.                     (*tmp->keyFunc)();
  135.                     redrawRequired = GL_TRUE;
  136.                     break;
  137.                 } else if ( auxDevice < tmp->key )
  138.                     tmp = tmp->right;
  139.                 else
  140.                     tmp = tmp->left;
  141.         }    break;
  142.  
  143.         default :
  144. #ifdef DEBUG2        
  145.             printf("   Toolkit : Default Event case ...\n");
  146. #endif
  147.             break;
  148.     }
  149. }
  150.  
  151.  
  152. /**************************************************************************
  153.  *   auxMainLoop() - set display function pointer
  154.  **************************************************************************/
  155.  
  156. GLvoid
  157. auxMainLoop( GLvoid (*_displayFunc)( GLvoid ) )
  158. {
  159.     GLint             device, value, mousex, mousey;
  160.  
  161.     displayFunc = _displayFunc;
  162.  
  163.     while ( GL_TRUE ) {
  164.         while ( auxEventPending() ||
  165.          ( animation == AUX_OFF && !redrawRequired && !idleFunc ) ) {
  166.             device = auxReadEvent( &value, &mousex, &mousey );
  167.  
  168.             if ( !(( device >= AUX_EXPOSE && device <= AUX_INPUTCHANGE ) ||
  169.              ( device >= AUX_LEFTBUTTON && device <= AUX_RIGHTBUTTON ) ||
  170.              ( device >= AUX_SHIFT && device <= AUX_ALT ) ||
  171.              device == AUX_NOEVENT) )
  172.                 device = AUX_KEYEVENT;
  173.  
  174.             auxDispatchEvent( device );
  175.         }
  176.  
  177.         if ( animation || redrawRequired ) {
  178.             (*displayFunc)();
  179.             redrawRequired = GL_FALSE;
  180.         }
  181.         if ( idleFunc ) {
  182.             (*idleFunc)();
  183.         }
  184.     }
  185. }
  186.  
  187.  
  188. /**************************************************************************
  189.  *   auxReshapeFunc() - set reshape function pointer
  190.  **************************************************************************/
  191.  
  192. GLvoid
  193. auxReshapeFunc( GLvoid (*_reshapeFunc)( GLsizei, GLsizei ) )
  194. {
  195.     reshapeFunc = _reshapeFunc;
  196. }
  197.  
  198. /**************************************************************************
  199.  *   auxExposeFunc() - set expose function pointer
  200.  *   Provided for compatibility with OpenGL Programming Guide libaux
  201.  **************************************************************************/
  202.  
  203. GLvoid
  204. auxExposeFunc( GLvoid (*_exposeFunc)( GLsizei, GLsizei ) )
  205. {
  206.     exposeFunc = _exposeFunc;
  207. }
  208.  
  209. /**************************************************************************
  210.  *   auxIdleFunc() - set idle function pointer
  211.  *   Provided for compatibility with OpenGL Programming Guide libaux
  212.  *   For animation of displayFunc, use auxAnimation() instead.
  213.  **************************************************************************/
  214.  
  215. GLvoid
  216. auxIdleFunc( GLvoid (*_idleFunc)( GLvoid ) )
  217. {
  218.     idleFunc = _idleFunc;
  219. }
  220.  
  221.  
  222.  
  223. /**************************************************************************
  224.  *   auxAnimation() - set animation function callback
  225.  **************************************************************************/
  226.  
  227. GLvoid
  228. auxAnimation( GLboolean state )
  229. {
  230.     animation = state;
  231. }
  232.  
  233.  
  234. /**************************************************************************
  235.  *   DefaultReshape() - Default Internal Reshape Function
  236.  **************************************************************************/
  237.  
  238. static GLvoid
  239. DefaultReshape( GLsizei width, GLsizei height )
  240. {
  241.     GLint      matrixMode;
  242.  
  243.     glViewport( 0, 0, width, height );
  244.     glGetIntegerv( GL_MATRIX_MODE, &matrixMode );
  245.     glMatrixMode( GL_PROJECTION );
  246.     glLoadIdentity();
  247.     glOrtho( -0.5, (GLfloat) width-0.5, -0.5, (GLfloat) height-0.5, -1.0, 1.0 );
  248.     glMatrixMode( matrixMode );
  249. }
  250.